package com.sea.data.controller;


import javax.xml.soap.*;
import java.io.ByteArrayOutputStream;
import java.util.Iterator;

public class SoapTestController {


    public static void main(String[] args) {
        try {
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();
            String url="http://zjfreight.yunwms.com//default/svc/web-service";
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

            SOAPBody soapBody = soapResponse.getSOAPBody();

            Iterator<?> responseIterator = soapBody.getChildElements();

            while (responseIterator.hasNext()) {
                Object responseObject = responseIterator.next();
                if (responseObject instanceof SOAPElement) {
                    SOAPElement responseElement = (SOAPElement) responseObject;
                    if (responseElement.getElementName().getLocalName().equals("callServiceResponse")) {
                        Iterator<?> callServiceResponseIterator = responseElement.getChildElements();
                        while (callServiceResponseIterator.hasNext()) {
                            Object callServiceResponseObject = callServiceResponseIterator.next();
                            if (callServiceResponseObject instanceof SOAPElement) {
                                SOAPElement callServiceResponseElement = (SOAPElement) callServiceResponseObject;
                                if (callServiceResponseElement.getElementName().getLocalName().equals("response")) {
                                    String jsonResponse = callServiceResponseElement.getValue().trim();
                                    System.out.println("JSON Response: " + jsonResponse);
                                }
                            }
                        }
                    }
                }
            }
            soapConnection.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private static SOAPMessage createSOAPRequest() throws Exception {
        // 创建一个消息工厂
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        // 获取SOAP部分
        SOAPPart soapPart = soapMessage.getSOAPPart();

        // 设置命名空间
        String namespace = "ns1";
        String namespaceURI = "http://www.example.org/Ec/";

        // 创建SOAP包
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration(namespace, namespaceURI);

        // 创建SOAP主体
        SOAPBody soapBody = envelope.getBody();
        SOAPElement callServiceElem = soapBody.addChildElement("callService", namespace);

        // 添加paramsJson元素
        SOAPElement paramsJsonElem = callServiceElem.addChildElement("paramsJson");
        paramsJsonElem.addTextNode("{\"pageSize\":\"1\",\"page\":1}");

        // 添加appToken元素
        SOAPElement appTokenElem = callServiceElem.addChildElement("appToken");
        appTokenElem.addTextNode("bc746e18a1d8a03718ed8d321e99444c");

        // 添加appKey元素
        SOAPElement appKeyElem = callServiceElem.addChildElement("appKey");
        appKeyElem.addTextNode("f28f771cb8d05cca6826e9aebb690252");

        // 添加service元素
        SOAPElement serviceElem = callServiceElem.addChildElement("service");
        serviceElem.addTextNode("getWarehouse");

        // 保存SOAP消息的更改
        soapMessage.saveChanges();

        // 打印SOAP请求
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        soapMessage.writeTo(out);
        String requestString = new String(out.toByteArray());
        System.out.println("Request SOAP Message:");
        System.out.println(requestString);

        return soapMessage;
    }

}
